﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using addemployeerecordcodefile;
using addqualificationcodefile;
//using MetriCam;

namespace Employee_Management_System
{
    public partial class AdminForm : Form
    {
        public int index = -1;
        LoginForm formLogin;
        EditForm formEdit;
        

        addemployee ae = new addemployee();
        addqualification aq = new addqualification();
        DataTable dtprofile, dtqualifications; 
        
        //public string reference="";
        public AdminForm(LoginForm parent)
        {
            formLogin = parent;
            dtprofile = new DataTable();
            dtqualifications = new DataTable();
            InitializeComponent();
            dtprofile.Columns.Add("Employee Number");
            dtprofile.Columns.Add("Employee Name");
            dtprofile.Columns.Add("Status");
            dtprofile.Columns.Add("Date Hired");
            dtprofile.Columns.Add("Department");
            dtprofile.Columns.Add("Position");
            dtprofile.Columns.Add("Contact Number");

            dtqualifications.Columns.Add("Qualification");

            blogout.Click += new EventHandler(btnlogout_Click);

            this.Load += new EventHandler(AdminForm_Load);
            txtsearch.KeyPress += new KeyPressEventHandler(txtsearch_KeyPress);
            lbresults.DoubleClick += new EventHandler(lbresults_DoubleClick);

            bviewprofile.Click += new EventHandler(btnviewprofile_Click);
            bviewqualification.Click += new EventHandler(btnviewqualification_Click);

            bevaluation.Click += new EventHandler(btnevaluation_Click);
            beditemployee.Click += new EventHandler(btnedit_Click);
            bevalresults.Click += new EventHandler(btnevalresults_Click);
            
        }
        void btnevaluation_Click(object sender, EventArgs e)
        {
            
        }
        void btnedit_Click(object sender, EventArgs e)
        {
            try
            {
                formEdit = new EditForm(formLogin, lbresults.SelectedIndex, lbresults.SelectedItem.ToString());
                this.Hide();
                formEdit.Show();
            }
            catch { MessageBox.Show("Please Select Employee to Edit."); }
        }

        void btnevalresults_Click(object sender, EventArgs e)
        {
            dgvreview.Visible = true;    
        } 

        void btnviewqualification_Click(object sender, EventArgs e)
        {
            dtqualifications.Clear();
            ae.dbQuery("SELECT fID, fQualifiedFields FROM tblQualification WHERE fEmployeeNumber='" + lbresults.SelectedItem.ToString() + "'");
            dgvqual.Visible = true;
            dgvqual.DataSource = dtqualifications;
            DataRow drq = dtqualifications.NewRow();

            //dr["Employee Number"] = ae.donResult.Rows[0]["fEmployeeNumber"].ToString();
            for (int i = 0; i < ae.donResult.Rows.Count; i++)
            {
                
                dtqualifications.Rows.Add(ae.donResult.Rows[i]["fQualifiedFields"]);
            }
            //
            dtprofile.Clear();
            ae.dbQuery("SELECT fEmployeeNumber, fEmployeeName, fEmployeeStatus, fDateHired, fDepartment, fPosition, fContact FROM tblProfiles WHERE fEmployeeNumber='" + lbresults.SelectedItem.ToString() + "'");
            dgvreview.Visible = true;
            dgvreview.DataSource = dtprofile;
            DataRow drprofile = dtprofile.NewRow();

            //dr["Employee Number"] = ae.donResult.Rows[0]["fEmployeeNumber"].ToString();
            for (int i = 0; i < ae.donResult.Rows.Count; i++)
            {

                drprofile["Employee Number"] = ae.donResult.Rows[i]["fEmployeeNumber"];
                drprofile["Employee Name"] = ae.donResult.Rows[i]["fEmployeeName"];
                drprofile["Status"] = ae.donResult.Rows[i]["fEmployeeStatus"];
                drprofile["Date Hired"] = ae.donResult.Rows[i]["fDateHired"];
                drprofile["Department"] = ae.donResult.Rows[i]["fDepartment"];
                drprofile["Position"] = ae.donResult.Rows[i]["fPosition"];
                drprofile["Contact Number"] = ae.donResult.Rows[i]["fContact"];
                dtprofile.Rows.Add(drprofile);
            }
           
        }
        //viewing employee profile by view profile button
        void btnviewprofile_Click(object sender, EventArgs e)
        {
            dgvqual.Visible = false;
            dtqualifications.Clear();
            dtprofile.Clear();
            ae.dbQuery("SELECT fEmployeeNumber, fEmployeeName, fEmployeeStatus, fDateHired, fDepartment, fPosition, fContact FROM tblProfiles WHERE fEmployeeNumber='" + lbresults.SelectedItem.ToString() + "'");
            dgvreview.Visible = true;
            dgvreview.DataSource = dtprofile;
            DataRow drprofile = dtprofile.NewRow();

            //dr["Employee Number"] = ae.donResult.Rows[0]["fEmployeeNumber"].ToString();
            for (int i = 0; i < ae.donResult.Rows.Count; i++)
            {

                drprofile["Employee Number"] = ae.donResult.Rows[i]["fEmployeeNumber"];
                drprofile["Employee Name"] = ae.donResult.Rows[i]["fEmployeeName"];
                drprofile["Status"] = ae.donResult.Rows[i]["fEmployeeStatus"];
                drprofile["Date Hired"] = ae.donResult.Rows[i]["fDateHired"];
                drprofile["Department"] = ae.donResult.Rows[i]["fDepartment"];
                drprofile["Position"] = ae.donResult.Rows[i]["fPosition"];
                drprofile["Contact Number"] = ae.donResult.Rows[i]["fContact"];
                dtprofile.Rows.Add(drprofile);
            }
            
        }
        //end viewing employee profile by view profile button

        //viewing the profile of the searched employee
        void lbresults_DoubleClick(object sender, EventArgs e)
        {
            dtprofile.Clear();
            dtqualifications.Clear();
            dgvqual.Visible = false;
            ae.dbQuery("SELECT fEmployeeNumber, fEmployeeName, fEmployeeStatus, fDateHired, fDepartment, fPosition, fContact FROM tblProfiles WHERE fEmployeeNumber='" + lbresults.SelectedItem.ToString() + "'");
            dgvreview.Visible = true;
            dgvreview.DataSource = dtprofile;
            DataRow drprofile = dtprofile.NewRow();
            
            //dr["Employee Number"] = ae.donResult.Rows[0]["fEmployeeNumber"].ToString();
            for (int i = 0; i < ae.donResult.Rows.Count; i++)
            {                
                drprofile["Employee Number"] = ae.donResult.Rows[i]["fEmployeeNumber"];
                drprofile["Employee Name"] = ae.donResult.Rows[i]["fEmployeeName"];
                drprofile["Status"] = ae.donResult.Rows[i]["fEmployeeStatus"];
                drprofile["Date Hired"] = ae.donResult.Rows[i]["fDateHired"];
                drprofile["Department"] = ae.donResult.Rows[i]["fDepartment"];
                drprofile["Position"] = ae.donResult.Rows[i]["fPosition"];
                drprofile["Contact Number"] = ae.donResult.Rows[i]["fContact"];
                dtprofile.Rows.Add(drprofile);
            }
        }
        //end of viewing the profile of the searched employee

        //searching employees
        void txtsearch_KeyPress(object sender, KeyPressEventArgs e)
        {  
            if (e.KeyChar == (char)13 && (radbtnempnum.Checked == false && radbtnsurname.Checked == false))
            {
                dgvreview.Visible = false;
                dgvqual.Visible = false;
                MessageBox.Show("Please specify search!");
            }
            else
            {
                if (e.KeyChar == (char)13 && radbtnempnum.Checked == true)
                {
                    dgvreview.Visible = false;
                    dgvqual.Visible = false;
                    ae.dbQuery("SELECT * FROM tblProfiles WHERE fEmployeeNumber='" + txtsearch.Text + "'");

                    if (ae.donResult.Rows.Count == 0)
                    {
                        MessageBox.Show("Employee number does not exist!");
                        MessageBox.Show("Please try other search options!");
                        lbresults.Items.Clear();
                        txtsearch.Text = "Search";
                        if (txtsearch.Text == "Search")
                        {
                            ae.dbQuery("SELECT * FROM tblProfiles");
                            lbresults.Items.Clear();
                            for (int i = 0; i < ae.donResult.Rows.Count; i++)
                            {
                                lbresults.Items.Add(ae.donResult.Rows[i]["fEmployeeNumber"]);
                            }
                        }
                    }
                    else
                    {
                        lbresults.Items.Clear();
                        for (int i = 0; i < ae.donResult.Rows.Count; i++)
                        {
                            lbresults.Items.Add(ae.donResult.Rows[i]["fEmployeeNumber"]);
                        }
                        txtsearch.Text = "Search";
                    }
                }
                if (e.KeyChar == (char)13 && radbtnsurname.Checked == true)
                {
                    dgvreview.Visible = false;
                    dgvqual.Visible = false;
                    ae.dbConnect();
                    ae.dbQuery("SELECT * FROM tblProfiles WHERE fSurname='" + txtsearch.Text + "'");

                    if (ae.donResult.Rows.Count == 0)
                    {
                        MessageBox.Show("Surname does not exist!");
                        MessageBox.Show("Please try other search options!");
                        lbresults.Items.Clear();
                        txtsearch.Text = "Search";
                        if (txtsearch.Text == "Search")
                        {
                            ae.dbQuery("SELECT * FROM tblProfiles");
                            lbresults.Items.Clear();
                            for (int i = 0; i < ae.donResult.Rows.Count; i++)
                            {
                                lbresults.Items.Add(ae.donResult.Rows[i]["fEmployeeNumber"]);
                            }
                        }
                    }
                    else
                    {
                        lbresults.Items.Clear();
                        for (int i = 0; i < ae.donResult.Rows.Count; i++)
                        {
                            lbresults.Items.Add(ae.donResult.Rows[i]["fEmployeeNumber"]);
                        }

                        txtsearch.Text = "Search";
                    }
                }
            } 
        }
        //end of searching employees
        
        void AdminForm_Load(object sender, EventArgs e)
        {
            radbtnempnum.Checked = true;
            ae.dbConnect();
            ae.dbQuery("SELECT * FROM tblProfiles");
            //aq.dbConnect();
            //aq.dbQuery("SELECT * FROM tblQualification");

            lbresults.Items.Clear();
            for (int i = 0; i < ae.donResult.Rows.Count; i++)
            {
                lbresults.Items.Add(ae.donResult.Rows[i]["fEmployeeNumber"]);
            }
        }

        void btnlogout_Click(object sender, EventArgs e)
        {
            formLogin = new LoginForm();
            this.Hide();
            formLogin.Show();
        }


        
    }
}
